import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import java.time.LocalDateTime

// 统计一天中每个小时的流量
object StatIpPerHour {
  def main(args: Array[String]): Unit = {
    // 1、创建SparkContext
    val conf = new SparkConf().setAppName(this.getClass.getCanonicalName.init).setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")
    val N = 3

    // 2、生成RDD
    val lines: RDD[String] = sc.textFile("file:///D:\\BaiduNetdiskDownload\\lagoubigdata\\fourthPhrase\\大数据正式班第四阶段模块一\\scala编程\\0-讲义和代码\\代码\\sparkPartOne\\data\\cdn.txt")


    // 2、统计每一个省份每一个小时的TOP3广告ID
    lines.map { line =>
      val fields: Array[String] = line.split("\\s+")
      var startIndex = fields(3).indexOf(":")
      var hour = fields(3).substring(startIndex+1,startIndex+3)
      (hour, 1)
    }.reduceByKey(_+_)
      .collect.foreach(println)



    // 5、关闭SparkContext
    import java.time.format.DateTimeFormatter
    //val dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy:HH:mm:ss")
    //val localDateTime = LocalDateTime.parse("15/Feb/2017:10:00:46", dateTimeFormatter)

    //println(localDateTime)
    sc.stop()
  }

  def getHour(str: String): Int = {
    import org.joda.time.DateTime

    val dt = new DateTime(str.toLong)
    dt.getHourOfDay
  }
}